/*
 * Copyright (C) 2003, Velocityme Partnership
 * TransitionBean.java
 *
 * Created on 05 May 2003, 09:17
 */

package com.velocityme.entity;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import com.velocityme.interfaces.*;
import com.velocityme.interfaces.ServiceUnavailableException;
import com.velocityme.valueobjects.RoleValue;
import com.velocityme.valueobjects.TransitionValue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 *
 * @author  Robert Crida Work
 * @ejb.bean
 *           type="CMP"
 *           cmp-version="2.x"
 *           name="Transition"
 *           jndi-name="ejb/Transition"
 *           view-type="local"
 * @ejb.transaction type="Required"
 *
 * @ejb.value-object match="*"
 *
 * @ejb.persistence table-name="transition"
 *
 * @ejb.util generate="physical"
 *
 * @ejb.finder signature="com.velocityme.Transition findStartTransitionByTaskStateMachine(com.velocityme.interfaces.TaskStateMachineLocal taskStateMachine)"
 *              query="SELECT OBJECT(o) FROM Transition o WHERE o.sourceStateLocal.nodeLocal.name = 'Start State' AND o.taskStateMachineLocal = ?1"
 *
 * @ejb.finder signature="java.util.Collection findBySourceStateAndTaskStateMachine(com.velocityme.interfaces.StateLocal source, com.velocityme.interfaces.TaskStateMachineLocal stateMachine)"
 *              query="SELECT OBJECT(o) FROM Transition o WHERE o.sourceStateLocal = ?1 AND o.taskStateMachineLocal = ?2"
 *
 * @jboss.persistence create-table="true"
 *                    remove-table="false"
 */
public abstract class TransitionBean implements EntityBean {
    
    /**
    * Context set by container
    */
    protected javax.ejb.EntityContext m_entityContext;
   
    public void setEntityContext(javax.ejb.EntityContext entityContext) {
        m_entityContext = entityContext;
    }

    public void unsetEntityContext() {
        m_entityContext = null;
    }

    /** @ejb.create-method */
    public com.velocityme.interfaces.TransitionPK ejbCreate(com.velocityme.valueobjects.TransitionValue p_value) throws CreateException {
        try {
            setTransitionId(generateNewID());
        }
        catch(ServiceUnavailableException e) {
            throw new EJBException(e.getMessage());
        }
        // EJB 2.0 spec says return null for CMP ejbCreate methods.
        return null;
    }

    public void ejbPostCreate(com.velocityme.valueobjects.TransitionValue p_value) throws CreateException {
        // CMR relations must be set in ejbPostCreate
        setTransitionValue(p_value);
    }

    /**
     * Retrive a unique creation id to use for this bean.  This will end up
     * demarcating this bean from others when it is stored as a record
     * in the database.
     *
     * @return Returns an integer that can be used as a unique creation id.
     *
     * @throws ServiceUnavailableException Indicating that it was not possible
     *                                     to retrieve a new unqiue ID because
     *                                     the service is not available
     **/
    private Integer generateNewID() throws ServiceUnavailableException {
        int lUniqueId = -1;
        try {            
            SequenceGenerator lBean = SequenceGeneratorUtil.getHome().create();
            lUniqueId = lBean.getNextNumber("transition","transitionId");
            lBean.remove();
        }
        catch ( NamingException ne ) {
            throw new ServiceUnavailableException( "Naming lookup failure: " + ne.getMessage() );
        }
        catch ( CreateException ce ) {
            throw new ServiceUnavailableException( "Failure while creating a generator session bean: " + ce.getMessage() );
        }
        catch ( RemoveException re ) {
            // When the Bean cannot be removed after a while it will be taken back by the container
            // therefore ignore this exception
        }
        catch ( RemoteException rte ) {
            throw new ServiceUnavailableException( "Remote exception occured while accessing generator session bean: " +  rte.getMessage() );
        }
        
        return new Integer(lUniqueId);
    }
    
    /**
     *  This gets a local object from a remote object.
     *  @ejb.interface-method view-type="remote"
     */
    public com.velocityme.interfaces.TransitionLocal getLocal() {
        return (TransitionLocal)m_entityContext.getEJBLocalObject();
    }
    
    /** @ejb.interface-method view-type="local" */
    public abstract com.velocityme.valueobjects.TransitionValue getTransitionValue();
    /** @ejb.interface-method view-type="local" */
    public abstract void setTransitionValue(com.velocityme.valueobjects.TransitionValue p_value);
    
    //=======================================================================
    //
    //                   ATTRIBUTES
    //
    //=======================================================================
      
    /**
     * @ejb.persistence column-name="transitionId"
     * @ejb.interface-method view-type="local"
     * @ejb.pk-field
     */
    public abstract java.lang.Integer getTransitionId();
    /** @ejb.interface-method view-type="local" */
    public abstract void setTransitionId(java.lang.Integer transitionId);

   /**
     * @ejb.persistence column-name="isOwnerResponsible"
     * @ejb.interface-method view-type="local"
     */
    public abstract java.lang.Boolean getIsOwnerResponsible();
    /** @ejb.interface-method view-type="local" */
    public abstract void setIsOwnerResponsible(java.lang.Boolean isOwnerResponsible);

   /**
     * @ejb.persistence column-name="isUserResponsible"
     * @ejb.interface-method view-type="local"
     */
    public abstract java.lang.Boolean getIsUserResponsible();
    /** @ejb.interface-method view-type="local" */
    public abstract void setIsUserResponsible(java.lang.Boolean isUserResponsible);

    //=======================================================================
    //
    //                   RELATIONSHIPS
    //
    //=======================================================================
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="TaskStateMachine-Transition"
     *               role-name="Transition-has-a-TaskStateMachine"
     * @jboss.relation fk-column="taskStateMachineIdk"
     *                 related-pk-field="taskStateMachineId"
     */
    public abstract com.velocityme.interfaces.TaskStateMachineLocal getTaskStateMachineLocal(); 
    /** @ejb.interface-method view-type="local" */
    public abstract void setTaskStateMachineLocal(com.velocityme.interfaces.TaskStateMachineLocal taskStateMachineLocal);
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="Transition-Status"
     *               role-name="Transition-has-a-Status"
     *               target-ejb="Status"
     *               target-role-name="Status-has-a-Transition"
     *               target-multiple="yes"
     * @jboss.relation fk-column="statusIdFk"
     *                 related-pk-field="statusId"
     * @ejb.value-object match="*" 
     *                   aggregate="com.velocityme.valueobjects.StatusValue"
     *                   aggregate-name="StatusValue"
     */
    public abstract com.velocityme.interfaces.StatusLocal getStatusLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setStatusLocal(com.velocityme.interfaces.StatusLocal statusLocal);
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="State-Transitions"
     *               role-name="Transition-has-a-SourceState"
     * @jboss.relation fk-column="sourceStateIdFk"
     *                 related-pk-field="stateId"
     * @ejb.value-object match="*" 
     *                   aggregate="com.velocityme.valueobjects.StateValue"
     *                   aggregate-name="SourceStateValue"
     */
    public abstract com.velocityme.interfaces.StateLocal getSourceStateLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setSourceStateLocal(com.velocityme.interfaces.StateLocal sourceStateLocal);
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="Transition-DestinationState"
     *               role-name="Transition-has-a-State"
     *               target-ejb="State"
     *               target-role-name="State-has-a-Transition"
     *               target-multiple="yes"
     * @jboss.relation fk-column="destinationStateIdFk"
     *                 related-pk-field="stateId"
     * @ejb.value-object match="*" 
     *                   aggregate="com.velocityme.valueobjects.StateValue"
     *                   aggregate-name="DestinationStateValue"
     */
    public abstract com.velocityme.interfaces.StateLocal getDestinationStateLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setDestinationStateLocal(com.velocityme.interfaces.StateLocal destinationStateLocal);
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="Transition-Role"
     *               role-name="Transition-may-have-many-ResponsibleRoles"
     * @jboss.relation fk-column="transitionId"
     *                 related-pk-field="roleId"
     * @jboss.relation-table table-name="responsibleroles"
     *                       create-table="true"
     *                       remove-table="false"
     * @ejb.value-object match="*" 
     *                   aggregate="com.velocityme.valueobjects.RoleValue"
     *                   aggregate-name="ResponsibleRoleValue"
     *                   members="com.velocityme.interfaces.RoleLocal"
     *                   members-name="RoleLocal"
     *                   relation="external"
     *                   type="Collection"
     */
    public abstract java.util.Collection getResponsibleRolesLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setResponsibleRolesLocal(java.util.Collection rolesLocal);
    
    //=======================================================================
    //
    //                   MANAGEMENT METHODS
    //
    //=======================================================================
    
}
